<?php

namespace WebmanGrpc\Examples\Transformer;

use WebmanGrpc\Transformer\ResponseTransformerInterface;
use Workerman\Protocols\Http\Response;
use Google\Protobuf\Internal\Message;

/**
 * 示例gRPC到HTTP响应转换器
 * 
 * 将gRPC响应转换为HTTP响应
 */
class ExampleResponseTransformer implements ResponseTransformerInterface
{
    /**
     * 转换gRPC响应为HTTP响应
     * 
     * @param Message $grpcResponse gRPC响应消息
     * @return Response HTTP响应
     */
    public function transform(Message $grpcResponse): Response
    {
        // 获取响应数据
        $data = [
            'message' => $grpcResponse->getMessage() ?? '',
            'code' => 0,
            'success' => true,
            'timestamp' => time(),
        ];
        
        // 返回JSON响应
        return new Response(200, [
            'Content-Type' => 'application/json'
        ], json_encode($data));
    }
}